package com.edianping.util;

import java.io.*;
//import java.util.List;
//import java.util.Vector;

import org.jdom.DataConversionException;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import com.edianping.config.AppConfigBean;
import com.edianping.config.DBInfoBean;
import com.edianping.config.DBReaderThreadConfigBean;
import com.edianping.config.DBWriterThreadConfigBean;
import com.edianping.config.ProcessThreadConfigBean;

/**
 * 读入配置文件
 * @author jszhang
 * 
 */

public class XmlUtil {

	public static DBInfoBean parseDBInfo(String DBInfoFile)
	{
		DBInfoBean dnBean = new DBInfoBean();
		
		File file = new File(DBInfoFile);
		Document doc = null;
		try {
			if (!file.exists() || file.isDirectory())
				return null;
			FileInputStream fis = new FileInputStream(file);				
			SAXBuilder builder = new SAXBuilder(false);
			doc = builder.build(fis);
			Element root  = doc.getRootElement();
			
			dnBean.dburl = root.getAttributeValue("dburl");
			dnBean.passwd =root.getAttributeValue("passwd");
			dnBean.username =root.getAttributeValue("username");
			fis.close();
		} catch (IOException ioe) {
			ioe.printStackTrace();
		} catch (JDOMException e) {		
			e.printStackTrace();
		}
		
		return dnBean;
		
	}
	public static AppConfigBean parse2wsb(String websitesFile) {
		File file = new File(websitesFile);
		Document doc = null;
		AppConfigBean wsb = null;
		try {
			if (!file.exists() || file.isDirectory())
				return null;
			FileInputStream fis = new FileInputStream(file);				
			SAXBuilder builder = new SAXBuilder(false);
			doc = builder.build(fis);
			wsb = doParse(doc);
			fis.close();
		} catch (IOException ioe) {
			ioe.printStackTrace();
		} catch (JDOMException e) {		
			e.printStackTrace();
		}
		return wsb;
	}
	
	private static AppConfigBean doParse(final Document doc){
		if(doc==null)
			return null;
		AppConfigBean wsb = new AppConfigBean();
		Element root  = doc.getRootElement();
		try {
			wsb.setConnpoosize( root.getAttribute("connpoolsize").getIntValue() );
			wsb.setProcessTaskCount( root.getAttribute("processTaskCount").getIntValue() );
			wsb.setDbReaderCount( root.getAttribute("dbReaderCount").getIntValue() );
			wsb.setDbWriterCount( root.getAttribute("dbWriterCount").getIntValue() );
			wsb.setThreadpoosize( root.getAttribute("threadPoolSize").getIntValue() );
			wsb.setInfoPath( root.getAttributeValue("infopath") );
			wsb.setMinShopProcessReviewCount( root.getAttribute("minShopProcessReviewCount").getIntValue() );
			wsb.setMinProductProcessReviewCount( root.getAttribute("minProductProcessReviewCount").getIntValue() );
			
			//other attribute parsed here
			Element pthread = root.getChild("processthread");
			ProcessThreadConfigBean pb = new ProcessThreadConfigBean();
			pb.setSleepperiod( Integer.parseInt( pthread.getChildText( "sleepperiod" ) ) );
			wsb.setProcessConfigBean( pb );
			
			Element dbrthread = root.getChild("dbreaderthread");
			DBReaderThreadConfigBean rb = new DBReaderThreadConfigBean();
			rb.setSleepperiod( Integer.parseInt( dbrthread.getChildText( "sleepperiod" ) ) );
			rb.setReadeachtime( Integer.parseInt( dbrthread.getChildText( "readeachtime" ) ) );
			wsb.setDbReaderConfigBean( rb );
			
			Element dbwthread = root.getChild("dbwriterthread");
			DBWriterThreadConfigBean wb = new DBWriterThreadConfigBean();
			wb.setSleepperiod( Integer.parseInt( dbwthread.getChildText( "sleepperiod" ) ) );
			wb.setWriteeachtime( Integer.parseInt( dbwthread.getChildText( "writeeachtime" ) ) );
			wb.setTakesleepperiod( Integer.parseInt( dbwthread.getChildText( "takesleepperiod" ) ) );
			wsb.setDbWriterConfigBean( wb );
		}
		catch (DataConversionException e) {			
			e.printStackTrace();
		}
		
		return wsb;
	}
	
	
	/**
	 * for Unit Test
	 */
	public static void main(String []args){
		String filename = "config.xml";
		AppConfigBean wsb = parse2wsb(filename);
		
		System.out.println( "test" );
	}

}
